package com.spider.gp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.spider.gp.constant.UrlConstant;
import com.spider.gp.dao.*;
import com.spider.gp.processor.GpPrePriceProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import us.codecraft.webmagic.Spider;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * description:
 *
 * @author clare
 * @date 2022/08/31 14:55
 **/
@RestController
@Slf4j
public class SuanfaController {

    @Autowired
    private GpPrePriceProcessor gpPrePriceProcessor;
    @Autowired
    private ZhiShuDao zhiShuDao;
    @Autowired
    private GpGzDao gpGzDao;
    @Autowired
    private GpSfJjDao gpSfJjDao;

    @Autowired
    private GpSfJjJzDao gpSfJjJzDao;

    /**
     * 针对均线 再平均得到一个更平滑的曲线  - 手动
     */
    public void pjpjjzSD() {
        String code = "002273";
//        LambdaQueryWrapper<GpSfJj> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(GpSfJj::getCode, code);
//        gpSfJjDao.delete(wrapper);
        LambdaQueryWrapper<GpSfJjJz> wrapper01 = new LambdaQueryWrapper<>();
        wrapper01.eq(GpSfJjJz::getCode, code);
//        wrapper01.eq(GpSfJjJz::getDayType, 8);
//        wrapper01.eq(GpSfJjJz::getDayType, 8);
        gpSfJjJzDao.delete(wrapper01);
//        pjpjAll("0", code);
        pjpjjz(3, code);
        pjpjjz(8, code);
    }

//    public static void main(String[] args) {
//        for (int i =1 ; i < 5 ; i++){
//            System.out.println(i);
//        }
//    }

    public void pjpjjz(int type, String code) {
//        //全部统计
//        LambdaQueryWrapper<GpGz> wrapperg = new LambdaQueryWrapper<>();
//        List<GpGz> gpGzList = gpGzDao.selectList(wrapperg);

//        for (GpGz gpGz : gpGzList) {
        LambdaQueryWrapper<GpSfJj> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GpSfJj::getCode, code);
        wrapper.eq(GpSfJj::getDayType, String.valueOf(type));
        wrapper.orderByDesc(GpSfJj::getReportTime);
        List<GpSfJj> gpSfJjList = gpSfJjDao.selectList(wrapper);

//            LambdaQueryWrapper<GpSfJjJz> wrapper01 = new LambdaQueryWrapper<>();
////            wrapper01.eq(GpSfJjJz::getCode, gpGz.getCode());
//            wrapper.eq(GpSfJj::getCode, "002273");
//            wrapper01.eq(GpSfJjJz::getDayType, String.valueOf(type));
//            wrapper01.orderByDesc(GpSfJjJz::getReportTime);
//            List<GpSfJjJz> gpSfJjJzs = gpSfJjJzDao.selectList(wrapper01);
//            if (gpSfJjJzs != null && gpSfJjJzs.isEmpty()) {
//                continue;
//            }
        for (GpSfJj gpSfJj : gpSfJjList) {
            Boolean flag = false;
            int i = 0;
            BigDecimal decimal = new BigDecimal(0);

            for (GpSfJj a : gpSfJjList) {
                if (a.getReportTime().equals(gpSfJj.getReportTime())) {
                    //循环到这个等于外面的时间了
                    flag = true;

                }
                if (flag) {
                    //说明已经走到对应的时间了 取最近的三个
                    decimal = decimal.add(new BigDecimal(a.getPerPrice()));
                    i++; //第一次多加了一次

                }
                if (i == type) {
                    //
                    break;
                }

            }
            if (i == type) {
                BigDecimal perPrice = decimal.divide(new BigDecimal(type), 2, BigDecimal.ROUND_HALF_UP);
                gpSfJjJzDao.insert(GpSfJjJz.builder().code(gpSfJj.getCode()).name(gpSfJj.getName()).dayType(gpSfJj.getDayType())
                        .reportTime(gpSfJj.getReportTime()).perPrice(perPrice.toPlainString()).curPrice(gpSfJj.getCurPrice()).build());
            }

//            }


        }


    }


    //TYPE 3 8

    public void pjpjjzToday(int type) {
        //全部统计
        LambdaQueryWrapper<GpGz> wrapperg = new LambdaQueryWrapper<>();
        List<GpGz> gpGzList = gpGzDao.selectList(wrapperg);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String reportTime = formatter.format(new Date());
        for (GpGz gpGz : gpGzList) {
            LambdaQueryWrapper<GpSfJj> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(GpSfJj::getCode, gpGz.getCode());
            wrapper.eq(GpSfJj::getDayType, String.valueOf(type));

//            wrapper.eq(GpSfJj::getReportTime, reportTime);
            wrapper.orderByDesc(GpSfJj::getReportTime);
            List<GpSfJj> gpSfJjList = gpSfJjDao.selectList(wrapper);

            LambdaQueryWrapper<GpSfJjJz> wrapper01 = new LambdaQueryWrapper<>();
            wrapper01.eq(GpSfJjJz::getCode, gpGz.getCode());
            wrapper01.eq(GpSfJjJz::getDayType, String.valueOf(type));
            wrapper01.orderByDesc(GpSfJjJz::getReportTime);
            List<GpSfJjJz> gpSfJjJzs = gpSfJjJzDao.selectList(wrapper01);
//            if (gpSfJjJzs != null && gpSfJjJzs.isEmpty()) {
//                continue;
//            }
//            for (GpSfJj gpSfJj : gpSfJjList) {
            Boolean flag = false;
            int i = 0;
            BigDecimal decimal = new BigDecimal(0);

            for (GpSfJj gpSfJj : gpSfJjList) {
                if (gpSfJj.getReportTime().equals(reportTime)) {
                    //循环到这个等于外面的时间了
                    flag = true;

                }
                if (flag) {
                    //说明已经走到对应的时间了 取最近的三个
                    decimal = decimal.add(new BigDecimal(gpSfJj.getPerPrice()));
                    i++;

                }
                if (i == type) {
                    BigDecimal perPrice = decimal.divide(new BigDecimal(type), 2, BigDecimal.ROUND_HALF_UP);

                    if (gpSfJjJzs != null && gpSfJjJzs.isEmpty()) {
                        LambdaUpdateWrapper<GpSfJjJz> updateWrapper = new LambdaUpdateWrapper<>();
                        updateWrapper.eq(GpSfJjJz::getReportTime, reportTime);
                        updateWrapper.eq(GpSfJjJz::getDayType, type);
                        updateWrapper.eq(GpSfJjJz::getCode, gpSfJj.getCode());
                        updateWrapper.set(GpSfJjJz::getCurPrice, gpSfJj.getCurPrice());
                        updateWrapper.set(GpSfJjJz::getPerPrice, perPrice.toPlainString());
                        gpSfJjJzDao.update(null, updateWrapper);
                    } else {
                        gpSfJjJzDao.insert(GpSfJjJz.builder().code(gpSfJj.getCode()).name(gpSfJj.getName()).dayType(gpSfJj.getDayType())
                                .reportTime(gpSfJj.getReportTime()).perPrice(perPrice.toPlainString()).curPrice(gpSfJj.getCurPrice()).build());
                    }

                    break;
                }

            }

//            }


        }


    }


    /**
     * 均线爬取 - 取最近三日的均价/3  取最近8日的均价/8  获取两条平滑的曲线 展示
     * 当日的
     */
    public void pjpjToday() {

        try {
            LambdaQueryWrapper<ZhiShu> wrapper = new LambdaQueryWrapper<>();
            List<ZhiShu> zhiShus = zhiShuDao.selectList(wrapper);
            LambdaQueryWrapper<GpGz> wrapperg = new LambdaQueryWrapper<>();
            List<GpGz> gpGzList = gpGzDao.selectList(wrapperg);

            //股票
            for (GpGz gpGz : gpGzList) {
//                String secid = gpGz.getRemark() + "." + gpGz.getCode();
                String secid = gpGz.getRemark() + "." + gpGz.getCode();
//                for (ZhiShu a : zhiShus){
//                    String reportTime = a.getReportTime().replace("-","");
                SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                String reportTime = formatter.format(new Date());
                String url3 = UrlConstant.DFCF__GP + "&lmt=3&secid=" + secid + "&end=" + reportTime; //得到的三日均线
                Spider.create(gpPrePriceProcessor).addUrl(url3).run();
                try {
                    TimeUnit.MILLISECONDS.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String url8 = UrlConstant.DFCF__GP + "&lmt=8&secid=" + secid + "&end=" + reportTime; //得到的8日均线
                Spider.create(gpPrePriceProcessor).addUrl(url8).run();

//                }
            }

        } catch (Exception e) {
            log.error("均线爬取爬虫失败： " + e.getMessage());
        }

    }


    public void pjpjAll(String remark, String code) {

        try {
            LambdaQueryWrapper<ZhiShu> wrapper = new LambdaQueryWrapper<>();
            List<ZhiShu> zhiShus = zhiShuDao.selectList(wrapper);


            //股票
//            for (GpGz gpGz : gpGzList) {
            String secid = remark + "." + code;
            for (ZhiShu a : zhiShus) {
                String reportTime = a.getReportTime().replace("-", "");

                LambdaQueryWrapper<GpSfJj> wrapper01 = new LambdaQueryWrapper<>();
                wrapper01.eq(GpSfJj::getCode, code);
                wrapper01.eq(GpSfJj::getReportTime, reportTime);
                Integer integer = gpSfJjDao.selectCount(wrapper01);
                if (integer != null && integer > 0) {
                    continue;
                }
//                    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//                    String reportTime = formatter.format(new Date());
                String url3 = UrlConstant.DFCF__GP + "&lmt=3&secid=" + secid + "&end=" + reportTime; //得到的三日均线
                Spider.create(gpPrePriceProcessor).addUrl(url3).run();
                try {
                    TimeUnit.MILLISECONDS.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String url8 = UrlConstant.DFCF__GP + "&lmt=8&secid=" + secid + "&end=" + reportTime; //得到的8日均线
                Spider.create(gpPrePriceProcessor).addUrl(url8).run();

            }
//            }

        } catch (Exception e) {
            log.error("均线爬取爬虫失败： " + e.getMessage());
        }

    }

}








